package Utils;

import Book.Book;
import com.bit.utils.FileUtils;
import constant.constant;
import java.io.IOException;
import java.time.LocalDate;

public class AnalyzingBook {
    //将数据存进⽂件
    public void storeObject(Book[] books, String filename) throws IOException {
        int useBookcount = 0;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null){
                useBookcount++;
            }
        }
        StringBuilder jsonArray =new StringBuilder();
        for (int i = 0; i < useBookcount; i++) {
            if (books[i] != null){
                jsonArray.append(books[i].toJson());
            }
            if (i != useBookcount - 1){
                jsonArray.append("\n");
            }
            FileUtils.writeFile(jsonArray.toString(),filename);
        }
    }
    //从⽂件读取数据
    public Book[] loadObject(String filename) throws IOException {
        String concent = FileUtils.readFile(filename);
        if (concent == null || concent.isEmpty()){
            System.out.println("没有数据"+ filename);
            return null;
        }
        String[] bookjson = concent.split("\n");
        Book[] booklist = new Book[bookjson.length];
        for (int i = 0; i < bookjson.length; i++) {
            Book book = parseBookJson(bookjson[i]);
            booklist[i] = book;
        }
        return booklist;
    }

    //创建书籍
    private Book parseBookJson(String json) {
        String[] fields = json.split(",");
        int bookId = Integer.parseInt(fields[0]);
        String title = fields[1];
        String author = fields[2];
        String category = fields[3];
        int publishYear = Integer.parseInt(fields[4]);
        boolean isBorrowed = Boolean.parseBoolean(fields[5]);
        int borrowCount = Integer.parseInt(fields[6]);
        LocalDate shelfDate = LocalDate.parse(fields[7]);
        if (title != null && author != null && category != null && publishYear != 0){
            Book book = new Book(title, author, category, publishYear, shelfDate);
            book.setBookId = bookId;
            book.setBorrowed(isBorrowed);
            book.setBorrowCount(borrowCount);
            return book;
        }
        return null;
    }

    public static void main(String[] args) {
        Book[] books = new Book[10];
        books[0] = new Book("三体", "刘慈欣", "科幻", 2015, LocalDate.of(2023, 1, 1));
        books[1] = new Book("三国演义", "罗贯中", "历史", 2015, LocalDate.of(2023, 1, 1));
        books[2] = new Book("红楼梦", "曹雪芹", "小说", 2015, LocalDate.of(2023, 1, 1));
        books[3] = new Book("西游记", "吴承恩", "小说", 2015, LocalDate.of(2023, 1, 1));
        AnalyzingBook analyzingBook = new AnalyzingBook();
        try {
//            analyzingBook.storeObject(books, constant.BOOK_NAME);
            Book[] cur = analyzingBook.loadObject(constant.FileBOOK_NAME);
            for (int i = 0; i < cur.length; i++) {
                System.out.println(cur[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
